package pl.mcnova.model.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import pl.mcnova.model.HibernateSession;
import pl.mcnova.model.entity.Contractor;
import pl.mcnova.model.entity.Mail;

public class MailDAO {

	private SessionFactory sessionFactory;
	private Session session;

	public MailDAO() {

		sessionFactory = HibernateSession.getInstance();

	}

	public void save(Mail mail) {
		session = sessionFactory.openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.save(mail);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				try {
					transaction.rollback();

				} catch (HibernateException he) {

				}
			}
		} finally {
			try {
				session.close();
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public void update(Mail mail) {
		session = sessionFactory.openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.update(mail);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				try {
					transaction.rollback();

				} catch (HibernateException he) {

				}
			}
		} finally {
			try {
				session.close();
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public void delete(Mail mail) {
		session = sessionFactory.openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.delete(mail);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				try {
					transaction.rollback();

				} catch (HibernateException he) {

				}
			}
		} finally {
			try {
				session.close();
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	@SuppressWarnings("finally")
	public Contractor get(Integer contractorID) {
		session = sessionFactory.openSession();
		Contractor contractor = null;

		try {

			contractor = (Contractor) session.get(Contractor.class, contractorID);

		} catch (Exception e) {

		} finally {
			try {
				session.close();
				return contractor;
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public List<Mail> getAll() {
		session = sessionFactory.openSession();

		List<Mail> mails = null;

		try {

			Criteria criteria = session.createCriteria(Mail.class);
			criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			mails = criteria.list();

		} catch (Exception e) {

		} finally {
			try {
				session.close();
				return mails;
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public List<Mail> getAllFrom(Integer contractorId) {
		session = sessionFactory.openSession();

		List mails = null;

		try {

			Query q = session.createSQLQuery("select * from mails where Contractor_Id = " + contractorId).addEntity(
					Mail.class);
			mails = q.list();

		} catch (Exception e) {

		} finally {
			try {
				session.close();
				return mails;
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

}
